package com.bw.trade.test;

import cn.hutool.core.bean.BeanUtil;
import com.bw.trade.TradeWebAPP;
import com.bw.trade.pojo.TbTransactionLog;
import com.bw.trade.service.TbTransactionLogService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.annotation.Reference;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = TradeWebAPP.class)
public class ReadFile {

    @Autowired
    private TbTransactionLogService tbTransactionLogService;

    @Test
    public void readTxtFile(String filePath) {

        clazzVo clazzVo = new clazzVo();
        try {
           String encoding = "UTF-8";
           File file = new File("E:\\b.txt");
           if (file.isFile() && file.exists()){ //判断文件是否存在
               InputStreamReader read = new InputStreamReader(
                       new FileInputStream(file), encoding);//考虑到编码格式
               BufferedReader bufferedReader = new BufferedReader(read);
               String lineTxt = null;
               while ((lineTxt = bufferedReader.readLine()) != null) {


                   String[] s=lineTxt.split("\\|");
                   BigDecimal big1 = new BigDecimal("" + 2.56);
                   clazzVo.setTransactionNo(s[0].toString());
                   clazzVo.setBillNo(s[1].toString());
                   BigDecimal big2 = new BigDecimal(s[2].toString());
                   clazzVo.setTransactionMoney(big2);
                   clazzVo.setTransactionStatus(Integer.valueOf(s[3]));
                   clazzVo.setTransactionStatusCode(s[4].toString());
                   clazzVo.setTransactionTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(s[5].toString()));
                   //System.out.println(lineTxt);


                   //System.out.println(clazzVo.toString());
                   TbTransactionLog tbTransactionLog = BeanUtil.copyProperties(clazzVo, TbTransactionLog.class);
                   tbTransactionLogService.save(tbTransactionLog);
                   System.out.println(tbTransactionLog.toString());

               }
               read.close();
           } else {
               System.out.println("找不到指定的文件");
           }
       } catch (Exception e) {
           System.out.println("读取文件内容出错");
           e.printStackTrace();
       }
    }
}